
package com.jiangxiaofei.citycloud.activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.util.StringUtil;
import com.jiangxiaofei.citycloud.view.ProgressDialogHelper;


/**
 * web网页公共类
 *
 * @author JiangXFei
 * @TODO android_bluesearemit
 * @2015-7-7
 */
@SuppressLint("JavascriptInterface")
public class WebActivity extends BaseActivity implements OnClickListener {
    protected WebView mWebview;
    private String url, title, type;
    private TextView tvTitle;

    //    private String url, title;
    //Android_Money?
    //http://192.168.1.231:81/Activity/Android_Money.aspx
    public class WebAppInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /**
         * Show a toast from the web page
         */
        // 如果target 大于等于API 17，则需要加上如下注解
        @JavascriptInterface
        public void HtmlcallJava(final String param) {
            if (StringUtil.isNotNull(param)) {
                if (param.equals("flush")) {
                    mWebview.post(new Runnable() {
                        @Override
                        public void run() {
                            mWebview.reload();
                        }
                    });
                }
                if (param.equals("bid")) {
                    finish();
                }
                if (param.equals("login")) {
                    //todo 交互登录
//                    Intent loginIntent = new Intent(WebActivity.this, LoginActivity.class);
//                    startActivity(loginIntent);
                    finish();
                }
            }
        }

        @JavascriptInterface
        public void HtmlcallJava2(final String param) {
            if (StringUtil.isNotNull(param)) {
                if (param.equals("money")) {

                }
            }
        }

        @JavascriptInterface
        public void LoginAndroid() {
            //todo 登录交互
//            Intent loginIntent = new Intent(WebActivity.this, LoginActivity.class);
//            startActivity(loginIntent);
        }

        @JavascriptInterface
        public void NewGuideRegister() {
            //todo 注册交互
//            Intent registerIntent = new Intent(WebActivity.this, RegisterActivity.class);
//            startActivity(registerIntent);
        }

        @JavascriptInterface
        public void ReturnInvestmentAndroid() {
            setResult(508);
            finish();
        }

        @JavascriptInterface
        public void SucessAndroid() {

        }
    }

    protected void initActionBar() {
        setLeftOneBg(R.drawable.ic_back);
        back_layout.setOnClickListener(this);
    }

    /**
     * 添加注入
     */
    protected void addJavaScript() {
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.app_base_web);
        mActivity = this;
        url = getIntent().getStringExtra("url");
        Toast.makeText(mContext,url,Toast.LENGTH_SHORT).show();
        title = getIntent().getStringExtra("title");
        type = getIntent().getStringExtra("type");
        initTitle();
        initActionBar();
        if (type != null && type.equals("heng")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            rl_layout.setVisibility(View.GONE);
        }else{
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            rl_layout.setVisibility(View.VISIBLE);
        }
//        ProgressDialogHelper.show(mContext);

    }

    @Override
    protected void setListener() {

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setCTitle(title);
        setLeftOneBg(R.drawable.ic_back);
        mWebview = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = mWebview.getSettings();

        webSettings.setJavaScriptEnabled(true);  //支持js

//        设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小


        webSettings.setSupportZoom(true);  //支持缩放，默认为true。是下面那个的前提。
        webSettings. setBuiltInZoomControls(true); //设置内置的缩放控件。
//        若上面是false，则该WebView不可缩放，这个不管设置什么都不能缩放。

        webSettings. setDisplayZoomControls(false); //隐藏原生的缩放控件

        webSettings. setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
        webSettings.supportMultipleWindows();  //多窗口
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  //关闭webview中缓存
        webSettings.setAllowFileAccess(true);  //设置可以访问文件
        webSettings.setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true);  //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        mWebview.addJavascriptInterface(new WebAppInterface(this), "jsObj");
        mWebview.removeJavascriptInterface("searchBoxJavaBridge_");
        mWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                result.cancel();
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                return super.onJsPrompt(view, url, message, defaultValue, result);
            }
        });

        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //setCTitle(view.getTitle());
                ProgressDialogHelper.dismissDialog();
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
        mWebview.loadUrl(url);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            default:
                break;
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    protected void callBackHandle(String url) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mWebview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebview.onPause();
    }
}
